package com.nmsl.web.controller.system;


import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.system.Utils.JwtUtil;
import com.nmsl.system.domain.JwtProperties;
import com.nmsl.system.domain.User;
import com.nmsl.system.domain.UserLoginDTO;
import com.nmsl.system.domain.vo.UserLoginVO;
import com.nmsl.system.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
public class UserLoginController {
    @Autowired
    private IUserService userService;
    @Autowired
    private JwtProperties jw;

    @PostMapping("/login")
    @ApiOperation("微信登录")
    public AjaxResult login(@RequestBody UserLoginDTO userLoginDTO){
        User user = userService.wxlogin(userLoginDTO);
        //生成jwt令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put("user_id",user.getId());
        String token = JwtUtil.createJWT(jw.getUserSecretKey(),jw.getUserTtl(),claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return AjaxResult.success(userLoginVO);
    }

}